/*
 * WelcomePanel.java
 *
 * Created on 15.5.2009, 19:49:40
 */
package org.marvec.pisnickar.panels;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.marvec.pisnickar.html.GlobalHtmlListener;
import org.marvec.pisnickar.html.HtmlListener;
import org.marvec.pisnickar.html.LocalHtmlRendererContext;
import org.marvec.pisnickar.html.ResourceProvider;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author marvec
 */
public class WelcomePanel extends javax.swing.JPanel {

    private static String WELCOME_PAGE_RES = "welcome.html";
    private static final long serialVersionUID = -6651588609954982432L;

    protected Reader getResource() throws UnsupportedEncodingException {
        return new StringReader(ResourceProvider.insertCss(ResourceProvider.loadResourceAsString(WELCOME_PAGE_RES)));
    }

    private void initHtmlPanel(HtmlListener listener) {
        try {
            UserAgentContext ucontext = new SimpleUserAgentContext();
            LocalHtmlRendererContext rcontext = new LocalHtmlRendererContext(htmlPanel1, ucontext);
            rcontext.addHtmlListener(listener);
            rcontext.setSupressOriginalHanlder(true);
            DocumentBuilderImpl dbi = new DocumentBuilderImpl(ucontext, rcontext);
            Document document = dbi.parse(new InputSourceImpl(getResource(), GlobalHtmlListener.INTERNAL_URL_PREFIX));
            htmlPanel1.setDocument(document, rcontext);
        } catch (SAXException ex) {
            Logger.getLogger(WelcomePanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WelcomePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** Creates new form WelcomePanel */
    public WelcomePanel(HtmlListener listener) {
        initComponents();
        initHtmlPanel(listener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        htmlPanel1 = new org.lobobrowser.html.gui.HtmlPanel();

        setName("Form"); // NOI18N

        htmlPanel1.setName("htmlPanel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(htmlPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(htmlPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.lobobrowser.html.gui.HtmlPanel htmlPanel1;
    // End of variables declaration//GEN-END:variables
}
